package com.gexiaobao.pigeon.ui.fragment.mine.training

import android.graphics.Color
import android.os.Bundle
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.model.bean.TrainingListResponse
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.FragmentMyTrainingBinding
import com.gexiaobao.pigeon.ui.adapter.MyTrainingListAdapter
import com.gexiaobao.pigeon.ui.dialog.RxDialogDefault
import com.gexiaobao.pigeon.ui.home.activity.ActivityTrainingPigeonList
import com.gexiaobao.pigeon.viewmodel.MyTrainingViewModel
import com.kingja.loadsir.core.LoadService
import com.yanzhenjie.recyclerview.OnItemMenuClickListener
import com.yanzhenjie.recyclerview.SwipeMenuCreator
import com.yanzhenjie.recyclerview.SwipeMenuItem
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2023/5/31 15:14
 * @Describe    : 我的训放
 */
class FragmentMyTraining : BaseFragment<MyTrainingViewModel, FragmentMyTrainingBinding>() {

    private val mAdapter: MyTrainingListAdapter by lazy { MyTrainingListAdapter(arrayListOf()) }
    private lateinit var loadsir: LoadService<Any>
    private var deleteTrainingData: ArrayList<TrainingListResponse.InfoList> = arrayListOf()

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.toolbar.initClose(getString(R.string.mine_fragment_my_training)) {
            nav().navigateUp()
        }
    }

    override fun initData() {
        super.initData()
        mDatabind.layoutList.swipeRefreshLayout.init {
            loadsir.showLoading()
            mViewModel.getTrainingList()
        }

        //状态页配置
        loadsir = loadServiceInit(mDatabind.layoutList.swipeRefreshLayout) {
            //点击重试时触发的操作
            loadsir.showLoading()
            mViewModel.getTrainingList()
        }

        val swipeMenuCreator = SwipeMenuCreator { _, rightMenu, _ ->
            val width = resources.getDimensionPixelSize(R.dimen.dp_70)
            val height = ViewGroup.LayoutParams.MATCH_PARENT
            val deleteItem = SwipeMenuItem(activity)
                .setBackground(R.drawable.round_b_g_5_train_fly_list_delete_red)
                .setTextColor(Color.WHITE)
                .setText(R.string.delete)
                .setTextSize(15)
                .setWidth(width)
                .setHeight(height)
            rightMenu.addMenuItem(deleteItem)
        }
        mDatabind.layoutList.swipeRecyclerView.setSwipeMenuCreator(swipeMenuCreator)

        val mItemMenuClickListener = OnItemMenuClickListener { menuBridge, position ->
            /** 任何操作必须先关闭菜单，否则可能出现Item菜单打开状态错乱 */
            menuBridge.closeMenu()
            /** 左侧还是右侧菜单 */
//            val direction = menuBridge.direction
//            /**  菜单在Item中的Position */
//            val menuPosition = menuBridge.position
            showDeleteDialog(position)
        }
        mDatabind.layoutList.swipeRecyclerView.setOnItemMenuClickListener(mItemMenuClickListener)
        mDatabind.layoutList.swipeRecyclerView.init(LinearLayoutManager(context), mAdapter).addItemDecoration(
            SpaceItemDecoration(0, Util.dp2px(context, 5f))
        )

        mAdapter.addChildClickViewIds(R.id.tvGps)
        mAdapter.setOnItemChildClickListener { _, _, position ->
            startActivity<ActivityTrainingPigeonList>("trainingId" to mAdapter.data[position].trainingId)
        }

        mAdapter.run {
            setOnItemClickListener { _, _, position ->
                nav().navigateAction(R.id.action_to_training_detail, Bundle().apply {
                    putInt("id", deleteTrainingData[position].trainingId)
                    putInt("type", 1)
                    putInt("trainingType", deleteTrainingData[position].trainingType)
                    putInt("trainingStatus", deleteTrainingData[position].trainingStatus)
                })
            }
        }
    }

    private fun showDeleteDialog(position: Int) {
        val dialog = RxDialogDefault(context)
        dialog.setTitle(getString(R.string.is_sure_delete_this_data))
        dialog.setSureListener {
            /** 调用删除接口 */
            mViewModel.deleteTraining(deleteTrainingData[position].trainingId.toString())
            dialog.dismiss()
        }
        dialog.setFullScreenWidth()
        dialog.show()
    }

    override fun onResume() {
        super.onResume()
        loadsir.showLoading()
        mViewModel.getTrainingList()
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.tvAddDeviceSubmit, mDatabind.tvDailyScan) {
            when (it) {
                mDatabind.tvAddDeviceSubmit -> nav().navigateAction(R.id.action_to_add_training)
                mDatabind.tvDailyScan -> nav().navigateAction(R.id.action_to_daily_scan)
            }
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.trainingListData.observe(viewLifecycleOwner) {
            loadListData(it, mAdapter, loadsir, mDatabind.layoutList.swipeRecyclerView, mDatabind.layoutList.swipeRefreshLayout)
            if (it.listData?.isEmpty() == false) {
                deleteTrainingData = it.listData
            }
        }

        mViewModel.deleteTraining.observe(viewLifecycleOwner) {
            if (it.isSuccess) {
                loadsir.showLoading()
                mViewModel.getTrainingList()
            } else {
                RxToast.showToast(it.errorMsg)
            }
        }
    }
}